package com.danawa.watcher.front.serviceStrategy;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.danawa.watcher.util.StringUtil;
import com.danawa.watcher.util.WatcherEnumUtil;
import com.danawa.watcherApi.database.domain.BaseProduct;
import com.danawa.watcherApi.database.domain.InflowProduct;
import com.danawa.watcherApi.database.domain.PriceHistory;
import com.danawa.watcherApi.database.domain.Type.State;
import com.danawa.watcherApi.database.service.WatcherService;
import com.danawa.watcherApi.parser.ShopParser;
import com.danawa.watcherApi.parser.ShopParserFactory;
import com.danawa.watcherApi.parser.ShopParserFactory.Checker;

public class WatcherServiceStrategy {
	
	@Autowired 
	WatcherService watcherService;
	
	
	/**
	 * 기준 상품 저장
	 * @param parserData
	 * @return
	 */
	public int setBaseProduct(Map<String, String> parserData ){
		int result = 0;
		BaseProduct baseProduct = new BaseProduct();
		baseProduct.setKey( parserData.get(ShopParser.PROD_CODE));
		baseProduct.setName(parserData.get(ShopParser.PROD_NAME));
		baseProduct.setImageUrl(parserData.get(ShopParser.PROD_IMAGE));
		baseProduct.setMallType(WatcherEnumUtil.mallType(parserData.get(ShopParser.SHOP_NAME)));
		baseProduct.setStateType(State.ON_SALE);
		
		try {
			result = watcherService.setBaseProduct(baseProduct);
			if(result > 0){
				result = baseProduct.getBaseProductSeq();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return result; 	
	}
	
	/**
	 * 유입상품  저장
	 * @param parserData
	 * @return
	 */
	public int setInflowProduct(int baseProductSeq, String siteUrl, Map<String, String> parserData ){
		int result = 0;
		
		if(baseProductSeq > 0){
			int basePrice = StringUtil.toInt(parserData.get(ShopParser.PROD_BASE_PRICE));
			int prodPrice = StringUtil.toInt(parserData.get(ShopParser.PROD_PRICE));
			InflowProduct inflowProduct = new InflowProduct();
			inflowProduct.setBaseProductSeq(baseProductSeq);
			inflowProduct.setSiteType(WatcherEnumUtil.siteType(""));
			inflowProduct.setLink(siteUrl);
			inflowProduct.setFixedPrice(basePrice);
			inflowProduct.setPresentPrice(prodPrice);
			inflowProduct.setMaximumPrice(prodPrice);
			inflowProduct.setMinimumPrice(prodPrice);
			inflowProduct.setTrackingCount(0);
			inflowProduct.setLikeCount(0);
			inflowProduct.setCreateDateTime(new Date());
			inflowProduct.setUpdateDateTime(new Date());
			
			try {
				result = watcherService.setInflowProduct(inflowProduct);
				if(result > 0){
					result = inflowProduct.getInflowProductSeq();
				}
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
		return result;
		
	}
	
	/**
	 * 가격 히스토리 저장
	 * @param parserData
	 * @return
	 */
	public int setPriceHistory(int inflowProductSeq, Map<String, String> parserData ){
		int result = 0;
		if(inflowProductSeq > 0){
			int prodPrice = StringUtil.toInt(parserData.get(ShopParser.PROD_PRICE));
			
			PriceHistory priceHistory = new PriceHistory();
			priceHistory.setInflowProductSeq(inflowProductSeq);
			priceHistory.setPrice(prodPrice);
			priceHistory.setInputDateTime(new Date());
			try {
				result = watcherService.setPriceHistory(priceHistory);
				if(result > 0){
					result = priceHistory.getPriceHistorySeq();
				}
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	/**
	 * url로 해당 데이타 파싱
	 * @param String
	 * @return
	 */
	public Map<String, String> dataParserMap(String url)  {
		Map<String, String> parserData = null;
		
		if(url != null && !"".equals(url)){
			ShopParser parser = ShopParserFactory.create(Checker.SHOP_ELEVEN);
			parser.addNext(ShopParserFactory.create(Checker.SHOP_SSG));
			try {
				parserData = parser.handler(url);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return parserData;
	}
}
